package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huawei.hmsdemo10086.Adapter.EvaluateAdapter;
import com.huawei.hmsdemo10086.POJO.Evaluate;
import com.huawei.hmsdemo10086.POJO.Goods;
import com.huawei.hmsdemo10086.POJO.Result;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class GoodsEvaluatesActivity extends AppCompatActivity {
    private ListView listView;
    private List<Evaluate> datas;
    EvaluateAdapter evaluateAdapter;
    private Goods goods;
    int flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_evaluates);
        flag = 0;
        TextView mTitleTextView = (TextView) this.findViewById(R.id.text_title);
        mTitleTextView.setText("全部评论");
        Button back = (Button)this.findViewById(R.id.button_backward);
        listView = (ListView) findViewById(R.id.goods_evaluate);
        datas = new ArrayList<Evaluate>();
        goods = (Goods) getIntent().getSerializableExtra("goods");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GoodsEvaluatesActivity.this,GoodsDetailsActivity.class);
                intent.putExtra("goods",goods);
                startActivity(intent);
                finish();
            }
        });

        listGoodsEvaluate();
        while(flag == 0)
        {
            System.out.println(1);
        }
        evaluateAdapter = new EvaluateAdapter(GoodsEvaluatesActivity.this,datas);
        listView.setAdapter(evaluateAdapter);
        evaluateAdapter.notifyDataSetChanged();
    }


    private void listGoodsEvaluate()
    {
        String url = "http://10.0.2.2:8443/api/listGoodsEvaluate";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"goodsid\": \"" + goods.getGoodsid() + "\"\n" +
                "}";

        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                String rep = response.body().string();
                System.out.println(rep.toString());

                Type type = new TypeToken<Result<List<Evaluate>>>(){}.getType();
                Result<List<Evaluate>> result = gson.fromJson(rep,type);


                datas = result.getData();
                flag = 1;


            }
        });
    }
}